package com.ts.enterprise.common.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;

import java.util.HashMap;
import java.util.Map;

public class SmsUtil {
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
    public static SendSmsRequest createRequest(String phone,String signName, String templateCode, String templateParamJson ) throws Exception {
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam(templateParamJson);
        return sendSmsRequest;
    }
    public static Map<String, String> sendAckInfo(com.aliyun.dysmsapi20170525.Client client,SendSmsRequest request) throws Exception {
    	Map<String, String> result = new HashMap<>(2);
        SendSmsResponse resp = client.sendSms(request);
        if (resp.getBody().code != null && resp.getBody().code.equals("OK")) {
            result.put("code", resp.getBody().code);
            result.put("bizId", resp.getBody().getBizId());
            return result;
        } else {
            result.put("code", resp.getBody().code);
            return result;
        }
    }
    public static boolean sendAckStatus(com.aliyun.dysmsapi20170525.Client client,SendSmsRequest request) throws Exception {
        SendSmsResponse resp = client.sendSms(request);
        if (resp.getBody().code != null && resp.getBody().code.equals("OK")) 
            return true;
         else 
            return false;
    }
}
